package com.css.fxfzdzzh.modules.mapManagement.controller;

import com.css.fxfzdzzh.base.response.RestResponse;
import com.css.fxfzdzzh.modules.mapManagement.param.MapTemplateQueryParam;
import com.css.fxfzdzzh.modules.mapManagement.service.MapTemplateService;
import com.css.fxfzdzzh.modules.mapManagement.vo.MapTemplateVO;
import com.css.fxfzdzzh.util.PlatformObjectUtils;
import com.css.fxfzdzzh.util.PlatformPageUtils;
import com.css.fxfzdzzh.util.ResponseCreator;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author Dbj
 * @date 2023年05月17日  16:50
 */
@RestController
@RequestMapping("/mapTemplate")
public class MapTemplateController {

    @Resource
    MapTemplateService mapTemplateService;

    /**
     * 图件模板上传保存
     */
    @PostMapping("/uploadMapTemplate")
    public RestResponse uploadMapTemplate(@Valid @RequestBody MapTemplateVO vo){
        mapTemplateService.uploadMapTemplate(vo);
        return ResponseCreator.createSuccessResponse("图件模板上传成功");
    }

    /**
     *  图件模板分页查询
     */
    @GetMapping("/mapTemplatePage")
    public RestResponse mapTemplatePage(MapTemplateQueryParam param, HttpServletRequest request){
        int curPage = Integer.parseInt(request.getParameter(PlatformPageUtils.CUR_PAGE));
        int pageSize=Integer.parseInt(request.getParameter(PlatformPageUtils.PAGE_SIZE));
        Map<String,Object> page=mapTemplateService.mapTemplatePage(param,curPage,pageSize);
        return ResponseCreator.createSuccessResponse(page);
    }

    /**
     *  图件模板批量删除(勾选)
     */
    @PostMapping("/delete")
    public RestResponse delete(@RequestBody Map<String,Object> map ) throws Exception {
        List<String> dmtIdList = (List<String>) map.get("dmtIdList");
        if(PlatformObjectUtils.isEmpty(dmtIdList) || dmtIdList.size()==0){
            throw new RuntimeException("请选择要删除的图件模板信息!");
        }
        mapTemplateService.delete(dmtIdList);
        return ResponseCreator.createSuccessResponse("删除成功!");
    }

    /**
     *  图件批量下载(勾选)
     */
    @PostMapping("/download")
    public void download(@RequestBody Map<String,Object> map, HttpServletResponse resp) throws Exception {
        List<String> attacheIdList = (List<String>) map.get("attachIdList");
        if(PlatformObjectUtils.isEmpty(attacheIdList) || attacheIdList.size()==0){
            throw new RuntimeException("请选择要下载的图件模板信息!");
        }
        mapTemplateService.download(attacheIdList,resp);
    }

    /**
     *  图件模板回显
     */
    @GetMapping("/mapTemplateView")
    public RestResponse mapTemplateView(@RequestParam String dmtId){
        return ResponseCreator.createSuccessResponse(mapTemplateService.getById(dmtId));
    }

}
